<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="网店编号" prop="tradingNum">
        <el-input
          v-model="queryParams.tradingNum"
          placeholder="请输入网店编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网店所属人" prop="shopHolder">
        <el-input
          v-model="queryParams.shopHolder"
          placeholder="请输入网店所属人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="网店名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入网店名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创店时间" prop="establishmentTime">
        <el-select v-model="queryParams.establishmentTime" placeholder="请选择创店时间" clearable
                   :style="{width: '100%'}">
          <el-option v-for="(item, index) in establishmentTimeOptions" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="好评率(%)" prop="favorableRate">
        <el-input
          v-model="queryParams.favorableRate"
          placeholder="请输入好评率(%)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="卖家到手价格" prop="handPrice">
        <el-input
          v-model="queryParams.handPrice"
          placeholder="请输入卖家到手价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="所在区域" prop="localArea">
        <el-select v-model="queryParams.localArea" placeholder="请选择所在区域" clearable :style="{width: '100%'}">
          <el-option v-for="dict in dict.type.s_province"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="主营类目" prop="mainCategory">
        <el-select v-model="queryParams.mainCategory" placeholder="请选择主营类目" clearable
                   :style="{width: '100%'}">
          <el-option v-for="dict in dict.type.trading_main_category"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="经营状态" prop="operatingState">
        <el-select v-model="queryParams.operatingState" placeholder="请选择经营状态" clearable
                   :style="{width: '100%'}">
          <el-option v-for="dict in dict.type.trading_operating_state"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="是否有差价" prop="priceDifference">
        <el-select v-model="queryParams.priceDifference" placeholder="请选择是否有差价" clearable
                   :style="{width: '100%'}">
          <el-option v-for="dict in dict.type.sys_yes_no"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="网店信誉等级" prop="reputationLevel">
        <el-select v-model="queryParams.reputationLevel" placeholder="请选择网店信誉等级" clearable
                   :style="{width: '100%'}">
          <el-option v-for="dict in dict.type.trading_reputation_level"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出售人姓名" prop="sellerName">
        <el-input
          v-model="queryParams.sellerName"
          placeholder="请输入出售人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网店出售价格" prop="sellingPrice">
        <el-input
          v-model="queryParams.sellingPrice"
          placeholder="请输入网店出售价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="店铺标签" prop="shopLabel">
        <el-input
          v-model="queryParams.shopLabel"
          placeholder="请输入店铺标签"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺来源" prop="shopSource">
        <el-select v-model="queryParams.shopSource" placeholder="请选择店铺来源" clearable :style="{width: '100%'}">
          <el-option v-for="dict in dict.type.trading_shop_source"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请输入淘信誉" prop="taobaoReputation">
        <el-input
          v-model="queryParams.taobaoReputation"
          placeholder="请输入请输入淘信誉"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--
      <el-form-item label="是否过户" prop="transferOrNot">
        <el-select v-model="queryParams.transferOrNot" placeholder="请选择是否过户" clearable
                   :style="{width: '100%'}">
          <el-option v-for="dict in dict.type.sys_yes_no"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="网店旺旺" prop="wangwangNum">
        <el-input
          v-model="queryParams.wangwangNum"
          placeholder="请输入网店旺旺"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['onlineShop:trading:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['onlineShop:trading:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['onlineShop:trading:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['onlineShop:trading:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="tradingList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="网店编号" align="center" prop="tradingNum" />
      <el-table-column label="网店所属人" align="center" prop="shopHolder"/>
      <el-table-column label="网店旺旺" align="center" prop="wangwangNum"/>
      <el-table-column label="网店名称" align="center" prop="shopName"/>
      <el-table-column width="150px" label="网店信誉等级" align="center" prop="reputationLevel">
        <template slot-scope="scope">
          <el-image
            style="width: 84px; height: 16px; align-content: center"
            :src= "scope.row.reputationLevel"
            fit="fill">
          </el-image>
<!--          <dict-tag :options="dict.type.trading_reputation_level" :value="scope.row.reputationLevel"/>-->
        </template>
      </el-table-column>
      <el-table-column label="淘信誉" align="center" prop="taobaoReputation"/>
      <el-table-column label="出售人姓名" align="center" prop="sellerName"/>
      <el-table-column label="网店出售价格" align="center" prop="sellingPrice"/>
<!--      <el-table-column label="优惠金额" align="center" prop="discountAmount"/>-->
      <el-table-column label="卖家到手价格" align="center" prop="handPrice"/>
      <el-table-column label="创店时间" align="center" prop="establishmentTime"/>
<!--      <el-table-column label="好评率(%)" align="center" prop="favorableRate"/>-->
      <el-table-column label="主营类目" align="center" prop="mainCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.trading_main_category" :value="scope.row.mainCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="经营状态" align="center" prop="operatingState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.trading_operating_state" :value="scope.row.operatingState"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺来源" align="center" prop="shopSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.trading_shop_source" :value="scope.row.shopSource"/>
        </template>
      </el-table-column>
      <el-table-column label="所在区域" align="center" prop="localArea">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.s_province" :value="scope.row.localArea"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleShow(scope.row)"
            v-hasPermi="['onlineShop:trading:query']"
          >查看
          </el-button>
          <el-button v-show="scope.row.dataStatus === '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAuction(scope.row)"
            v-hasPermi="['onlineShop:trading:auction']"
          >拍下
          </el-button>
          <el-button v-show="scope.row.dataStatus === '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['onlineShop:trading:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['onlineShop:trading:adminRemove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改trading对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="${comment}" prop="createUserId">
          <el-input v-model="form.createUserId" placeholder="请输入${comment}"/>
        </el-form-item>
        <el-form-item label="${comment}" prop="createUserName">
          <el-input v-model="form.createUserName" placeholder="请输入${comment}"/>
        </el-form-item>
        <el-form-item label="${comment}" prop="updateUserId">
          <el-input v-model="form.updateUserId" placeholder="请输入${comment}"/>
        </el-form-item>
        <el-form-item label="${comment}" prop="updateUserName">
          <el-input v-model="form.updateUserName" placeholder="请输入${comment}"/>
        </el-form-item>
        <el-form-item label="QQ号" prop="qqNum">
          <el-input v-model="form.qqNum" placeholder="请输入QQ号"/>
        </el-form-item>
        <el-form-item label="历史假冒扣分-分" prop="counterfeitScore">
          <el-input v-model="form.counterfeitScore" placeholder="请输入历史假冒扣分-分"/>
        </el-form-item>
        <el-form-item label="历史假冒扣分-年" prop="counterfeitYear">
          <el-input v-model="form.counterfeitYear" placeholder="请输入历史假冒扣分-年"/>
        </el-form-item>
<!--
<el-form-item label="优惠金额" prop="discountAmount">
          <el-input v-model="form.discountAmount" placeholder="请输入优惠金额"/>
        </el-form-item>-->
        <el-form-item label="创店时间" prop="establishmentTime">
          <el-input v-model="form.establishmentTime" placeholder="请输入创店时间"/>
        </el-form-item>
        <el-form-item label="好评率(%)" prop="favorableRate">
          <el-input v-model="form.favorableRate" placeholder="请输入好评率(%)"/>
        </el-form-item>
        <el-form-item label="卖家到手价格" prop="handPrice">
          <el-input v-model="form.handPrice" placeholder="请输入卖家到手价格"/>
        </el-form-item>
        <el-form-item label="网店链接" prop="link">
          <el-input v-model="form.link" placeholder="请输入网店链接"/>
        </el-form-item>
        <el-form-item label="所在区域" prop="localArea">
          <el-input v-model="form.localArea" placeholder="请输入所在区域"/>
        </el-form-item>
        <el-form-item label="主营类目" prop="mainCategory">
          <el-input v-model="form.mainCategory" placeholder="请输入主营类目"/>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="经营状态" prop="operatingState">
          <el-input v-model="form.operatingState" placeholder="请输入经营状态"/>
        </el-form-item>
        <el-form-item label="是否有差价" prop="priceDifference">
          <el-input v-model="form.priceDifference" placeholder="请输入是否有差价"/>
        </el-form-item>
        <el-form-item label="网店信誉等级" prop="reputationLevel">
          <el-input v-model="form.reputationLevel" placeholder="请输入网店信誉等级"/>
        </el-form-item>
        <el-form-item label="出售人姓名" prop="sellerName">
          <el-input v-model="form.sellerName" placeholder="请输入出售人姓名"/>
        </el-form-item>
        <el-form-item label="网店出售价格" prop="sellingPrice">
          <el-input v-model="form.sellingPrice" placeholder="请输入网店出售价格"/>
        </el-form-item>
        <el-form-item label="网店地址" prop="shopAddress">
          <el-input v-model="form.shopAddress" placeholder="请输入网店地址"/>
        </el-form-item>
        <el-form-item label="网店详情" prop="shopDetails">
          <el-input v-model="form.shopDetails" placeholder="请输入网店详情"/>
        </el-form-item>
        <el-form-item label="店铺标签" prop="shopLabel">
          <el-input v-model="form.shopLabel" placeholder="请输入店铺标签"/>
        </el-form-item>
        <el-form-item label="网店名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入网店名称"/>
        </el-form-item>
        <el-form-item label="店铺来源" prop="shopSource">
          <el-input v-model="form.shopSource" placeholder="请输入店铺来源"/>
        </el-form-item>
        <el-form-item label="请输入淘信誉" prop="taobaoReputation">
          <el-input v-model="form.taobaoReputation" placeholder="请输入请输入淘信誉"/>
        </el-form-item>
<!--        <el-form-item label="是否过户" prop="transferOrNot">
          <el-input v-model="form.transferOrNot" placeholder="请输入是否过户"/>
        </el-form-item>-->
        <el-form-item label="网店旺旺" prop="wangwangNum">
          <el-input v-model="form.wangwangNum" placeholder="请输入网店旺旺"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <showShop ref="showShop" :dataForm="dataForm" @updateDataList="handleQuery"></showShop>
  </div>

</template>

<script>
import {listTrading, getTrading, delTrading, addTrading, updateTrading, tradingAuction} from "@/views/shop/api/trading";
import ShowShop from "@/views/shop/onlineShop/trading/showShop.vue";

export default {
  components :{
    ShowShop,
  },
  name: "Trading",
  dicts: ['sys_yes_no', 'trading_shop_source', 'trading_main_category', 'trading_shop_address',
    'trading_operating_state', 'trading_reputation_level', 's_province'],
  data() {
    function getYearsArray() {
      // 获取当前年份
      const currentYear = new Date().getFullYear();
      // 初始化一个空数组来存储年份对象
      let yearsArray = [];
      // 循环20次，从当前年份向前推20年
      for (let i = 0; i < 20; i++) {
        // 计算年份
        const year = currentYear - i;
        // 创建一个对象，并添加到数组中
        yearsArray.push({
          "label": `${year}年`,
          "value": `${year}年`
        });
      }
      return yearsArray;
    }
    return {
      currentView: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // trading表格数据
      tradingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dataStatus: null,
        createUserId: null,
        createUserName: null,
        shopHolder: null,
        updateUserId: null,
        updateUserName: null,
        qqNum: null,
        counterfeitScore: null,
        counterfeitYear: null,
        discountAmount: null,
        establishmentTime: null,
        favorableRate: null,
        handPrice: null,
        link: null,
        localArea: null,
        mainCategory: null,
        mobile: null,
        operatingState: null,
        priceDifference: null,
        reputationLevel: null,
        sellerName: null,
        sellingPrice: null,
        shopAddress: null,
        shopDetails: null,
        shopLabel: null,
        shopName: null,
        shopSource: null,
        shopType: null,
        taobaoReputation: null,
        transferOrNot: null,
        type: null,
        wangwangNum: null,
        imageUrl: null,
        tradingNum: null
      },
      // 表单参数
      form: {},
      dataForm :{},
      // 表单校验
      rules: {},
      establishmentTimeOptions: getYearsArray(),
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询trading列表 */
    getList() {
      this.loading = true;
      listTrading(this.queryParams).then(response => {
        this.tradingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        createUserId: null,
        createUserName: null,
        shopHolder: null,
        updateTime: null,
        updateUserId: null,
        updateUserName: null,
        qqNum: null,
        counterfeitScore: null,
        counterfeitYear: null,
        discountAmount: null,
        establishmentTime: null,
        favorableRate: null,
        handPrice: null,
        link: null,
        localArea: null,
        mainCategory: null,
        mobile: null,
        operatingState: null,
        priceDifference: null,
        reputationLevel: null,
        sellerName: null,
        sellingPrice: null,
        shopAddress: null,
        shopDetails: null,
        shopLabel: null,
        shopName: null,
        shopSource: null,
        shopType: null,
        taobaoReputation: null,
        transferOrNot: null,
        type: null,
        wangwangNum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加trading";
    },
    /** 查看按钮操作 */
    handleShow(row) {
      const id = row.id || this.ids
      getTrading(id).then(response => {
        this.dataForm = response.data;
        this.dataForm.disabledType = true;
        this.$refs.showShop.openShow();
        //this.title = "查看网店";
      });
    },
    /** 拍下按钮操作 */
    handleAuction(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认拍下所选店铺？').then(function () {
        return tradingAuction(id);
      }).then(response => {
        this.getList();
      }).catch((err) => {

      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      getTrading(id).then(response => {
        this.dataForm = response.data;
        this.dataForm.disabledType = false;
        this.$refs.showShop.openShow();
        //this.title = "查看网店";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTrading(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTrading(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(row.dataStatus === '0' ? '是否确认删除所选数据项？' : '删除已拍下的网店将同步删除订单及绩效！是否继续？').then(function () {
        return delTrading(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('onlineShop/trading/export', {
        ...this.queryParams
      }, `trading_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.right-popup {
  /* 初始位置在屏幕右侧之外 */
  transform: translateX(100%);
  transition: transform 0.3s ease-out;
}

/* 当Dialog可见时，将其从右侧滑入 */
.right-popup.el-dialog--visible {
  transform: translateX(0);
}

/* 调整Dialog的位置，使其靠右显示 */
.right-popup .el-dialog {
  margin: 0;
  position: fixed;
  top: 50px; /* 根据需要调整 */
  right: 0;
  border-radius: 0 6px 6px 0; /* 右侧圆角 */
}

/* 可选：调整Dialog内部内容的位置，确保内容不会紧贴边缘 */
.right-popup .el-dialog__body {
  padding: 20px;
}

/* 可选：自定义Dialog的footer样式 */
.dialog-footer {
  text-align: right;
}
</style>
